//工具0
var log = function () {
    console.log.apply(console, arguments)
}
//作业1：参数是一个只包含数字的array，求array的乘积
var project = function (array) {
    var s = 1
    for (var i = 0; i < array.length; i++) {
        var n = array[i]
        s = s * n
    }
    return s
}
//工具1
var ensure = function (condition, message) {
    if (!condition) {
        console.log(message)
    }
}
// 测试用例：
var testProject = function () {
    ensure(project([1, 2, 3]) === 6, 'test product 1')
    ensure(project([1, 2, 0]) === 0, 'test product 2')
}

testProject()
//作业2：返回一个数的绝对值
var abs = function (n) {
    if (n < 0) {
        return -n
    } else {
        return n
    }
}

var testAbs = function () {
    ensure(abs(0) === 0,)
}

testAbs()

//时间
var currentTime = function () {
    var d = new Date()
    var month = d.getMonth() + 1
    var date = d.getDate()
    var hours = d.getHours()
    var minutes = d.getMinutes()
    var seconds = d.getSeconds()
    var timeString = `${month}/${date} ${hours}:${minutes}:${seconds}`
    return timeString
}

//工具2
var ensureEqual = function (a, b, message) {
    if (a != b) {
        log(`*** 测试失败，${a} 不等于 ${b},${message}`)
    }
}
//函数：根据key得到value,没有找到对应的value，使用默认值

var valueForKey = function (object, key, defaultValue) {
    var value = object[key]
    if (value === undefined) {
        value = defaultValue
    }
    return value
}

var discount = function (price, grade) {
    var grades = [
        '小学生',
        '大学生',
    ]
    var priceMap = {
        '小学生': 0.5,
        '大学生': 0.9,
    }

    var dis = valueForKey(priceMap, grade, 1)

    if (grades.includes(grade)) {
        return price * dis
    } else {
        return price
    }
}

//